/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.command
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import multimedia.control.event.UpdatePreferencesEvent;
	import multimedia.model.ModelLocator;
	
	/**
	 * 	UpdatePreferencesCommand
	 * 
	 *	@author jemden
	 *	@version 1.0
	 */
	public class UpdatePreferencesCommand extends SequenceCommand
	{
		private var model:ModelLocator;
		
		private var e:UpdatePreferencesEvent;
		
		override public function execute(event:CairngormEvent):void
		{
			this.model = ModelLocator.getInstance();
			this.e = event as UpdatePreferencesEvent;
			this.nextEvent = e.nextEvent;
			
			var prefsFile:File; // The preferences prefsFile
			prefsFile = File.applicationStorageDirectory;
			prefsFile = prefsFile.resolvePath(ModelLocator.XML_FILE_NAME); 
			
			var prefsXML:XML = <preferences/>;
			prefsXML.webservice.@url = e.url;
			prefsXML.webservice.@api_key = e.apiKey;
			prefsXML.webservice.@player_id = e.playerId;
			prefsXML.webservice.@fullscreen_on_startup = e.fullscreenOnStartup;
			prefsXML.webservice.@debug = e.debug;
			prefsXML.webservice.@showNewsticker = e.showNewsticker;
			prefsXML.webservice.@traceLogging = e.traceLogging;
			prefsXML.webservice.@fileLogging = e.fileLogging;
			prefsXML.webservice.@logLevel = e.logLevel;
			prefsXML.saveDate = new Date().toString();
			
			var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
			outputString += prefsXML.toXMLString();
			outputString = outputString.replace(/\n/g, File.lineEnding);
			
			var stream:FileStream = new FileStream();
			stream.open(prefsFile, FileMode.WRITE);
			stream.writeUTFBytes(outputString);
			stream.close();
			
			executeNextCommand();
		}
	}
}